//分页条
function initPage(pageNo, totalCount, arr){
    // 获取分页器实例对象
    var myPager = $('#myPager').data('zui.pager');
    //设置当前页码为 4，并同时设置记录总数为 100， 每页记录数目为 30
    myPager.set(pageNo, totalCount, 5);
    renderConsIntro(arr);
}
$('#myPager').pager({
    onPageChange: function(state, oldState) {
        if (state.page !== oldState.page) {
            // console.log('页码从', oldState.page, '变更为', state.page);
            queryBtn(state.page);
        }
    }
});

//下拉框
var optionList = [];
getFieldList();
function getFieldList(){
    $.ajax({
        url:config.url + "/getFieldList",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        type:'GET' ,
        success:function(resp) {
            addOptionList(resp.data);
        },
        error:function() {
        }
    });
}

function addOptionList(arr){
    for (let i = 0; i < arr.length; i++) {
        optionList.push( {text: arr[i].fieldName + '', value: arr[i].id + '', keys: 'fields'});
    }
    $('#picker6').picker({list: optionList});

}

getUserInfo();
function getUserInfo(){
    $.ajax({
        url:config.url + "/getUserInfo",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        headers:{
            "token":localStorage.getItem("token")
        },
        crossDomain: true,
        type:'GET' ,
        success:function(resp) {
            if(resp.data != null){
                $("#login-reg-btn")[0].innerHTML = "欢迎 " + resp.data;
                queryBtn(1);
            }else{
                alert("请先登录");
                $("#login-reg-btn")[0].innerHTML = "登录/注册";
            }
        },
        error:function() {
        }
    });
}

//获取咨询师列表
var fieldList = []

function fieldObj(fieldName){
    this.fieldName = fieldName;
}


function queryBtn(i){
    var username = parent.document.getElementById('login-reg-btn').innerText;
    $.ajax({
        url:config.url + "/order/orderList",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            username:username.split(" ")[1],
            pageNo:i
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            arr = resp.data;
            initPage(arr.pageNo,arr.totalCount,arr);
        },
        error:function() {
        }
    });
}
let arr;
function renderConsIntro(arr){
    console.log(arr)

    let str = '';
    for (let i = 0; i < arr.list.length; i++) {
        let b = arr.list[i];
        if(b.subStatus == '已诊断'){
            str += `<div class="item flex cons-info-item">
                    <div><img src="img/index/user.jpg"></div>
                    <div class="intro">
                        <div>
                         <span>订单号: </span>${b.orderId}&nbsp;&nbsp;
</div>
                        <div>
                           
                            <span>咨询人: </span>${b.username}&nbsp;&nbsp;
                            <span>领域: </span>${b.fieldName}&nbsp;&nbsp;
                            <span>预约时间: </span>${b.subTime}&nbsp;&nbsp;
                        </div>
                        <div>
                            <span>预约状态: </span>${b.subStatus}&nbsp;&nbsp;
                            <span>费用: </span>${b.cost}&nbsp;&nbsp;&nbsp;
                            <span>咨询师: </span>${b.consName}
                        </div>
                        <div></div>
                        <div><span>问题描述: </span>${b.questionInfo}</div>
                        <div><span>诊断答复: </span>${b.answer}</div>
                        <div><span>答复时间: </span>${b.answerTime == null || b.answerTime == '' ? "暂未诊断":b.answerTime}</div>
                        <div><span>评价内容: </span>${b.evaluate == null || b.evaluate == '' ? "暂无评价":b.evaluate}</div>

                    </div>
                    <div class="item-btn">
                        <button class="btn btn-success searchBtn btn-lg btn-primary" 
                        data-toggle="modal" data-target="#myModal2"
                        type="button" onclick="evaluateBtn(${b.id},'${b.consName}','${b.orderId}')">点击评价</button></div>
                    </div>`;
            $('#items-wrap')[0].innerHTML = str;
        }else{

            str += `<div class="item flex cons-info-item">
                    <div><img src="img/index/user.jpg"></div>
                    <div class="intro">
                        <div>
                         <span>订单号: </span>${b.orderId}&nbsp;&nbsp;
</div>
                        <div>
                           
                            <span>咨询人: </span>${b.username}&nbsp;&nbsp;
                            <span>领域: </span>${b.fieldName}&nbsp;&nbsp;
                            <span>预约时间: </span>${b.subTime}&nbsp;&nbsp;
                        </div>
                        <div>
                            <span>预约状态: </span>${b.subStatus}&nbsp;&nbsp;
                            <span>费用: </span>${b.cost}&nbsp;&nbsp;&nbsp;
                            <span>咨询师: </span>${b.consName}
                        </div>
                        <div></div>
                        <div><span>问题描述: </span>${b.questionInfo}</div>
                        <div><span>诊断答复: </span>${b.answer}</div>
                        <div><span>答复时间: </span>${b.answerTime == null || b.answerTime == '' ? "暂未诊断":b.answerTime}</div>
                        <div><span>评价内容: </span>${b.evaluate == null || b.evaluate == '' ? "暂无评价":b.evaluate}</div>

                    </div>
                    <div class="item-btn">
                        <button class="btn btn-success searchBtn btn-lg btn-primary" 
                        data-toggle="modal" data-target="#myModal3"
                        type="button" onclick="evaluateBtn(${b.id},'${b.consName}','${b.orderId}')">点击评价</button></div>
                    </div>`;
            $('#items-wrap')[0].innerHTML = str;
        }
    }
}
var orderIdGlobe;
function evaluateBtn(id,consName,orderId){
    console.log(consName);
    orderIdGlobe = orderId;
    $.ajax({
        url: config.url + "/order/getConsInfo",
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        data: {
            consName: consName
        },
        crossDomain: true,
        type: 'GET',
        success: function (resp) {
            console.log(resp);
            var consInfo = resp.data;
            let a = consInfo.consultantFieldVos;
            let field = "";
            for (let j = 0; j < a.length; j++) {
                field += a[j].fieldName + "&nbsp;"
            }
            let str = '';
            str += `
        <div class="flex">
                   <div><img src="img/index/cons1.JPG" id="cons-img"></div>
                   <div>
                       <ul class="list-group">
                           <li class="list-group-item">咨询师姓名:&nbsp;&nbsp;${consInfo.name}</li>
                           <li class="list-group-item">${consInfo.school}&nbsp;&nbsp;
                            ${consInfo.positionalTitles}</li>
                           <li class="list-group-item">擅长领域:&nbsp;&nbsp;${field}</li>
                           <li class="list-group-item">专业背景:&nbsp;&nbsp;${consInfo.professionalBackground}</li>
                       </ul>
                       <div class="answer-div" id="evaluate-inp">
                           评价
                       </div>
    
                       <div class="input-control has-label-left">
                           <input id="inputAccountExample4" type="text" class="form-control" placeholder="">
                           <label for="inputAccountExample3"  class="input-control-label-left text-right">内容:</label>
                       </div>
                   </div>
               </div>`

            $('#modal-body-id')[0].innerHTML = str;
        }
    });
}

// var optionList2 = []
// var consId;
// function subBtn(id){
//     consId = id;
//     $('#modal-body').val(id);
//     var name = '';
//     for (let i = 0; i < arr.list.length; i++) {
//         let a = arr.list[i].consultantFieldVos;
//         b  = arr.list[i];
//         if (b.id == id){
//             name = b.name;
//             for (let j = 0; j < a.length; j++) {
//                 optionList2.push( {text: a[j].fieldName + '', value: a[j].id + '', keys: 'fields'});
//             }
//             $('#picker0').picker({list: optionList2});
//             break;
//         }
//     }
//     $('#consName').val(name);
//     //通过用户id获取日期时间
//     getDateTimeInfo(id);
// }

function getDateTimeInfo(id){
    $.ajax({
        url:config.url + "/timeInfo",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            consId:id
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            var temp = JSON.parse(resp.data);
            renderDate(temp);
        }
    });
}
function renderDate(arr){
    var str1 = `<option value="">请选择</option>`;
    for (let i = 0; i < arr.length; i++) {
        var str = arr[i].dateTime
        let dateAndTime = str.split("-");
        console.log(dateAndTime)
        let year = parseInt(dateAndTime[0]);
        let month = parseInt(dateAndTime[1]);
        let day = parseInt(dateAndTime[2]);
        let hour = parseInt(dateAndTime[3]);
        // list2.push(str);
        let date = year + '-' + month + '-' + day + ' ' + hour +':00';
        str1 += `<option value="${date}">${date}</option>`;
    }
    $('#picker3')[0].innerHTML = str1;
}

function submitEvaluateForm(){
    let evaluate = $('#inputAccountExample4').val();
    $.ajax({
        url:config.url + "/order/evaluate",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            //评价内容
            evaluate:evaluate,
            orderId:orderIdGlobe
            //预约id
        },
        crossDomain: true,
        type:'POST',
        success:function(resp) {
            console.log(resp);
            alert(resp.msg);
            location.reload();
        }
    });
}